package com.zcsy.manage.web.system;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.sms.dlife.DlifeSmsConfig;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.commons.util.Globals;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.OwnerHouse;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.mall.IntegralConfig;
import com.zcsy.manage.model.mall.IntegralRecord;
import com.zcsy.manage.model.system.DistrictInfo;
import com.zcsy.manage.model.system.Menu;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.model.system.bo.RegisterInfo;
import com.zcsy.manage.persistence.weixin.StringUtil;
import com.zcsy.manage.service.base.ICellInfoService;
import com.zcsy.manage.service.base.IOwnerHouseService;
import com.zcsy.manage.service.base.IShopInfoService;
import com.zcsy.manage.service.base.IVerifyCodeService;
import com.zcsy.manage.service.mall.IIntegralConfigService;
import com.zcsy.manage.service.mall.IIntegralRecordService;
import com.zcsy.manage.service.system.IDistrictInfoService;
import com.zcsy.manage.service.system.IMenuService;
import com.zcsy.manage.service.system.IUserService;
import com.zcsy.manage.web.common.LogUtils;
import com.zcsy.manage.web.common.SixCodeUtil;

import net.sf.json.JSONArray;

/**
 * 
 * @Title: LoginController.java
 * @Package： com.zcsy.manage.web.system 
 * @Description: 登陆方法
 * @author chensf 735569870@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年1月8日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
public class LoginController {

    /**
     * 日志创建对象<br>
     */
    //private static final Log log = LogFactory.getLog(LoginController.class);
    
    @Autowired
    private IUserService userService;
    
    @Autowired
    private ICellInfoService cellInfoService;
    
    @Autowired
    private IVerifyCodeService verifyCodeService;
    
    @Autowired
    private IDistrictInfoService districtInfoService;
    
    @Autowired
    private IOwnerHouseService ownerHouserService;
    
    @Autowired
    private IShopInfoService shopInfoService;
    
    @Autowired
    private IMenuService menuService;
    
    @Autowired
    private IIntegralRecordService integralRecordService;
    
    @Autowired
    private IIntegralConfigService IntegralConfigService;
    
    private static final int USER_CHECK = 1;
        
    private static final int PHONE_BIND = 2;
   
    private static final int NEW_SHOP = 1;
    
    @RequestMapping(value="login" , method = RequestMethod.GET)
    @ResponseBody
    public RequestJson login(String uname,String pwd,HttpServletRequest request,
            HttpServletResponse response) {
        final HttpSession session = request.getSession();
        RequestJson result = new RequestJson();
        try {
            //验证登录名和手机号
            User loginUser = new User();
            loginUser.setPhone(uname);
            //Constants.OWNER_TYPE
            loginUser.setUserType(Constants.OWNER_TYPE);
            User user = userService.userLogin(loginUser);
            if (user == null) {
                result.setMsg("用户不存在");
                result.setSuccess(false);
                return result;
            }
            if(user.getState().intValue() != 1){
                result.setMsg("用户已停用");
                result.setSuccess(false);
                 return result;
            }
            //先设置一个默认小区id
            List<CellInfo> cellList = ownerHouserService.getCellInfoListByOwnerId(user.getId());
            if(null == cellList || cellList.size() <= 0) {
                result.setMsg("用户无审核通过的小区");
                result.setSuccess(true);
                result.setObj(user);
                return result;
            }
            
            //前台用手机号或登录名加密的，但是数据库是用登录名加密存储的，所以要筛除掉手机号的情况
//            pwd = DESEncryption.decrypt(pwd, uname);
//            pwd = DESEncryption.encrypt(pwd, user.getLoginname());
//            final String userKey = MD5Util.generalPassword(user.getLoginname(), pwd);
            //密码校验 
            if(pwd.equals(user.getPwd())){
                
                // 获取用户注册的小区信息
                result.setSuccess(true);
                result.setObj(user);
                
                // 设置session超时时间
                session.setMaxInactiveInterval(RequestConstants.SessionActionTime);
                
                session.setAttribute(RequestConstants.OWNER_CELL_INFO, cellList.get(0));
                session.setAttribute(RequestConstants.LAST_LOGIN_TIME, user.getLastLoginTime());
                
                user.setLastLoginTime(new Date());
                user.setCurrentCell(cellList.get(0));
                userService.updateByPrimaryKeySelective(user);
                session.setAttribute(RequestConstants.USER_SESSION_ID, user);
                session.setAttribute(RequestConstants.USER_ID, user.getId());
                session.setAttribute(RequestConstants.IMAGE_LOGO, Globals.getImageUrl());
                LogUtils.insertLog("用户登录",user.getId(),request);
                
                // 根据唯一标识name获取积分配置信息
                IntegralConfig integralConfig = IntegralConfigService.selectByName("Login");
                if (null != integralConfig) {
                    // 首次登录可获得的积分
                    Integer integralLogin = Integer.valueOf(integralConfig.getValue());

                    // new一个积分变更记录
                    IntegralRecord integralRecord = new IntegralRecord();

                    // 检查用户是否是第一次登录，是：增加100积分。
                    if (null != user.getLoginTimes() && user.getLoginTimes() == Constants.NEW_REGISTERER) {
                        // 更新用户总积分信息
                        user.setIntegral(null == user.getIntegral() ? 0 : user.getIntegral() + (null == integralLogin ? 0 : integralLogin));
                        userService.updateByPrimaryKeySelective(user);

                        // 插入积分变更信息
                        integralRecord.setId(UniqueNumberUtils.get18UniqueNumber());
                        integralRecord.setDescription("新用户首次登录获得积分");
                        integralRecord.setChange(integralLogin);
                        integralRecord.setUserAccount(user.getLoginname());
                        integralRecord.setRecordTime(DateUtils.getDate());
                        integralRecordService.insert(integralRecord);
                    }
                }
                // 用户登录一次就改变登录次数
                user.setLoginTimes(user.getLoginTimes() + 1);
                user.setLastLoginTime(new Date());
                userService.updateByPrimaryKeySelective(user);
            }else{
                result.setMsg("密码错误");
                result.setSuccess(false);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(RequestConstants.NET_ERROR);
            result.setSuccess(false);
        }
        return result;
    }
    
    @RequestMapping(value="exist" , method = RequestMethod.GET)
    @ResponseBody
    public RequestJson logout(HttpServletRequest request,HttpServletResponse response) {
        RequestJson result = new RequestJson();
        
        final HttpSession session = request.getSession();
        User user = (User)session.getAttribute(RequestConstants.USER_SESSION_ID);
        LogUtils.insertLog("用户退出",user.getId(),request);
        session.setAttribute(RequestConstants.USER_SESSION_ID, null);
        session.setAttribute(RequestConstants.OWNER_CELL_INFO, null);
        //退出之前清除与当前用户关联的小区seesion
        if(session.getAttribute("roleCells") != null && session.getAttribute("roleCells") != ""){
            session.removeAttribute("roleCells");
        }
        session.invalidate();
        return result;
    }
    
    /**
     * @Description 返回登录后选择小区页面<br>
     * 
     * @param model
     * @param request
     * @param response
     * @return 
     * @author caiyinli 13277030520@163.com
     * @date 2016年5月16日
     * @see 
     */
    @RequestMapping(value="selectCell" , method = RequestMethod.GET)
    public String selectCell(Model model,HttpServletRequest request,HttpServletResponse response) {
        return "selectCell";
    }
    
    /**
     * @Description 通过省id获取与用户绑定的小区所在市市集合<br>
     * 
     * @param provinceId 省id
     * @param request 用户请求
     * @param response 服务器响应
     * @return 返回与用户绑定的小区所在市市集合
     * @author caiyinli 13277030520@163.com
     * @date 2016年5月16日
     * @see 
     */
    @RequestMapping(value="getCityListByProvinceId" , method = RequestMethod.GET)
    @ResponseBody
    public RequestJson getCityListByProvinceId(Integer provinceId,HttpServletRequest request,HttpServletResponse response) {
        RequestJson result = new RequestJson();
        final HttpSession session = request.getSession();
        @SuppressWarnings("unchecked")
        List<String> roleCells =  (List<String>) session.getAttribute("roleCells");
        try {
            List<CellInfo> infos = cellInfoService.getCityListByProvinceId(provinceId, roleCells);
            infos.get(0).setCells(cellInfoService.getCellListByCityId(infos.get(0).getCityId(), roleCells));
            result.setObj(JSONArray.fromObject(infos).toString());
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * @Description 通过市id获取与用户绑定的小区所在小区集合<br>
     * 
     * @param cityId 城市id
     * @param request用户请求
     * @param response服务器响应
     * @return 返回与用户绑定的小区集合
     * @author caiyinli 13277030520@163.com
     * @date 2016年5月16日
     * @see 
     */
    @RequestMapping(value="getCellListByCityId" , method = RequestMethod.GET)
    @ResponseBody
    public RequestJson getCellListByCityId(Integer cityId,HttpServletRequest request,HttpServletResponse response) {
        RequestJson result = new RequestJson();
        final HttpSession session = request.getSession();
        @SuppressWarnings("unchecked")
        List<String> roleCells =  (List<String>) session.getAttribute("roleCells");
        try {
            List<CellInfo> infos = cellInfoService.getCellListByCityId(cityId, roleCells);
            
            result.setObj(JSONArray.fromObject(infos).toString());
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
     /**
     * 
     * @Description 返回商户身份验证页面
     * 
     * @param model
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年6月13日
     * @see
     */
    @RequestMapping(value = "userCheck", method = RequestMethod.GET)
    public String userCheck(Model model, HttpServletRequest req) {
        try {
            User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
                model.addAttribute("user", user);
                String phone = user.getPhone();
                if(StringUtils.isNotEmpty(phone)){
                model.addAttribute("type", USER_CHECK);
                } else {
                    model.addAttribute("type", PHONE_BIND);    
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        model.addAttribute("resendTime", DlifeSmsConfig.SMS_RESEND_TIME);
        
        return "store/bindphone/bindPhone";
        
    }
    
    /**
     * 
     * @Description 返回绑定手机页面
     * 
     * @param model
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年6月13日
     * @see
     */
    @RequestMapping(value = "bindPhone", method = RequestMethod.GET)
    public String bindPhone(Model model, HttpServletRequest req) {
        model.addAttribute("type", PHONE_BIND);
        model.addAttribute("resendTime", DlifeSmsConfig.SMS_RESEND_TIME);
        
        return "store/bindphone/bindPhone";
        
    }
    
    /**
     * 
     * @Description 返回手机绑定完成页面
     * 
     * @param model
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年6月13日
     * @see
     */
    @RequestMapping(value = "bindComplete", method = RequestMethod.GET)
    public String bindComplete(Model model, HttpServletRequest req) {
        model.addAttribute("type", PHONE_BIND);
        
        return "store/bindphone/bindComplete";
    }
    
    /**
     * 
     * @Description 绑定手机发送验证码信息
     * 
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年6月12日
     * @see
     */
    @RequestMapping(value = "sendOldPhone", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson sendMSG(HttpServletRequest req){
        RequestJson result = new RequestJson();
        try{
             User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
             String phone = user.getPhone();
             //随机生成六位数验证码
             String phoneCode = SixCodeUtil.sixCode();
             
             user.setUserType(Constants.OWNER_TYPE);
             verifyCodeService.sendCodeMessage(user, phone, phoneCode, result);
             
        }  catch (Exception e) {
             result.setSuccess(false);
             result.setMsg("操作失败");
             e.printStackTrace();
        }

        return result;
        
    }
    
    /**
     * 
     * @Description 绑定新手机号发送验证码
     * 
     * @param phone 新手机号
     * @param id 主键id
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年6月14日
     * @see
     */
    @RequestMapping(value = "sendNewPhone", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson savaPhone(String phone, String id, HttpServletRequest req) {
        RequestJson result = new RequestJson();
        try{
            User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
            user.setPhone(phone);
            user.setId(id);
            userService.updateByPrimaryKeySelective(user);
            //随机生成六位数验证码
            String phoneCode = SixCodeUtil.sixCode();
            user.setUserType(Constants.OWNER_TYPE);
            verifyCodeService.sendCodeMessage(user, phone, phoneCode, result);
            
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg("操作失败");
        }
        
        return result;
    }
    
    /**
     * 
     * @Description 判断输入的登录名是否存在<br>
     * 
     * @param loginName 登录名
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年6月21日
     * @see
     */
    @RequestMapping(value="isUser" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson isUser(String loginName,HttpServletRequest req){
        RequestJson result = new RequestJson();
        try {
            User user = userService.selectByPhone(loginName);
            if (user != null ){
                result.setObj(user.getPhone());

            } else {
                result.setObj(loginName);
                
            }
            result.setSuccess(true);
            result.setMsg("操作成功");
            
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("操作失败");
            e.printStackTrace();
        }
        
        return result;
     
}
    
    /**
     * 
     * @Description 返回找回密码页面<br>
     * 
     * @param model
     * @param loginName 用户登录名
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年6月21日
     * @see
     */
    @RequestMapping(value = "findPassword", method = RequestMethod.GET)
    public String findPassword( Model model,String loginName, HttpServletRequest req) {
        try{
            if(StringUtils.isNotEmpty(loginName)){
            	String loginname = userService.selectLoginNameByPhone(loginName);
                User user = userService.selectByPhone(loginname);
                model.addAttribute("user", user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        model.addAttribute("resendTime", DlifeSmsConfig.SMS_RESEND_TIME);
        
        return "common/findPassword";
        
    }
    
    
   
    @RequestMapping(value = "sendRegisterCode", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson sendRegisterCode(String phone, HttpServletRequest req){
        RequestJson result = new RequestJson();
        try{
             User user = new User();
             //随机生成六位数验证码
             String phoneCode = SixCodeUtil.sixCode();
             user.setUserType(Constants.OWNER_TYPE);
             verifyCodeService.sendCodeMessage(user, phone, phoneCode, result);
                 
        }  catch (Exception e) {
             result.setSuccess(false);
             result.setMsg("操作失败");
             e.printStackTrace();
        }
        
        return result;
    }
    
    @RequestMapping(value = "checkPhone", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson checkPhone(String phone, HttpServletRequest request){
        RequestJson result = new RequestJson();
        try{
             User user = userService.selectUserByPhoneAndUserType(phone, Constants.USER_OWNER);
             if(user != null) {
                 result.setSuccess(false);
                 result.setMsg("手机号已注册");   
             }
        }  catch (Exception e) {
             result.setSuccess(false);
             result.setMsg("操作失败");
             e.printStackTrace();
        }
        
        return result;
    }
    
    /**
     * 
     * @Description 忘记密码发送验证码修改密码<br>
     * 
     * @param name 登录名
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年6月16日
     * @see
     */
    @RequestMapping(value = "sendCode", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson sendCode(String lname, HttpServletRequest req){
        RequestJson result = new RequestJson();
        try{
             User user = userService.selectByPhone(lname);
             if (user != null){
                 String phone = user.getPhone();
                 //随机生成六位数验证码
                 String phoneCode = SixCodeUtil.sixCode();
                 
                 user.setUserType(Constants.OWNER_TYPE);
                 verifyCodeService.sendCodeMessage(user, phone, phoneCode, result);
                 
             }
        }  catch (Exception e) {
             result.setSuccess(false);
             result.setMsg("操作失败");
             e.printStackTrace();
        }
        
        return result;
    }
    
    /**
     * 
     * @Description 保存新密码<br>
     * 
     * @param loginName 用户登录名
     * @param newPwd 新密码
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年6月21日
     * @see
     */
    @RequestMapping(value="completePassword" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveOrUpdateAdInfo(String loginName,String newPwd,HttpServletRequest req){
        RequestJson result = new RequestJson();
        User user = userService.selectByPhone(loginName);
        user.setPwd(newPwd);
        try {
            //生成密匙
//            final String newUserKey = MD5Util.generalPassword(loginName,newPwd);
//            user.setPwd(newPwd);
            userService.updateByPrimaryKeySelective(user);
            
            result.setSuccess(true);
            result.setMsg("操作成功");
            
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("操作失败");
            e.printStackTrace();
        }
        
        return result;
     
    }
    
    @RequestMapping(value = "register", method = RequestMethod.GET)
    public String register(Model model, HttpServletRequest req) {
        try{
            List<DistrictInfo> provinces = districtInfoService.getDistrictInfoByLevel(Constants.DISTRICT_LEVEL_FIRST);
            model.addAttribute("provinces", provinces);
        } catch (Exception e) {
        }
        return "common/register";
        
    }
    
    @RequestMapping(value = "registerCellHouser", method = RequestMethod.GET)
    public String registerCellHouser(Model model, String userId, HttpServletRequest req) {
        try{
            User user = userService.selectByPrimaryKey(userId);
            model.addAttribute("user", user);
            
            List<DistrictInfo> provinces = districtInfoService.getDistrictInfoByLevel(Constants.DISTRICT_LEVEL_FIRST);
            model.addAttribute("provinces", provinces);
        } catch (Exception e) {
        }
        return "common/registerCellHouser";
    }
    
    @RequestMapping(value="saveRegister" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveRegister(RegisterInfo info, HttpServletRequest req) {
        RequestJson result = new RequestJson();
        try{
        	User user = userService.selectUserByPhoneAndUserType(info.getPhone(), Constants.USER_OWNER);
            if(user != null) {
                result.setSuccess(false);
                result.setMsg("手机号已注册");   
            } else {
              // 校验房屋是否已注册
              if(!StringUtil.isEmptyString(info.getUserId())) {
                  List<OwnerHouse> list = ownerHouserService.getOwnerHouserByHouseAndOwner(info.getHouseId(), info.getUserId());
                  if(null != list && list.size() > 0) {
                      result.setSuccess(false);
                      result.setMsg("房屋已注册，请等待审核");
                  }
              } else {
                  //生成密匙
//                final String newUserKey = MD5Util.generalPassword(info.getPhone(), info.getPassword());
                  info.setPassword(info.getPassword());
                  userService.registerInfo(info);
              }
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("操作失败");
        }
        return result;
    }
    
    /**
     * @description 我要开店-小区分类
     * @param parentId 父级（市）id
     * @author chenxin
     * @date 2016年8月11日下午5:30:27
     */
    @RequestMapping(value = "cellList", method = RequestMethod.GET)
    @ResponseBody
    public RequestJson cellList(Model model, String city) {
        RequestJson result = new RequestJson();
        try {
            List<CellInfo> cellInfos = cellInfoService.getCells(city);

            result.setSuccess(true);
            result.setObj(cellInfos);
        } catch (Exception e) {
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * @description 保存“我要开店”基本信息
     * String 营业执照填写页面
     * @author chenxin
     * @date 2016年8月11日下午2:04:24
     *//*
    @RequestMapping(value = "nextStep", method = RequestMethod.POST)
    public String nextStep(Model model, String yourName, String yourPhone, Integer province, Integer city, Integer area,
            String describe, String shop, String cellId, HttpServletRequest request) {

        try {
            ShopInfo shopInfo = new ShopInfo();
//            shopInfo.setId(UniqueNumberUtils.get18UniqueNumber());
            shopInfo.setContactor(yourName);
            shopInfo.setContactorPhone(yourPhone);
            shopInfo.setProvinceId(province);
            shopInfo.setCityId(city);
            shopInfo.setAreaId(area);
            shopInfo.setCellId(cellId);
//            User user = (User)request.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
//            CellInfo cellInfo = user.getCurrentCell();
//            if(null != cellInfo) {
//                shopInfo.setCellId(cellInfo.getId());
//            }
            shopInfo.setDescrip(describe);
            shopInfo.setShopCategoryId(shop);

            model.addAttribute("shopInfo", shopInfo);
//            shopInfoService.insert(shopInfo);
            
        } catch (Exception e) {
            
        }
        return "openShop";
    }*/
    
    /**
     * @description 跳转到营业执照填写页面
     * @author chenxin
     * @date 2016年8月12日上午11:25:00
     */
    /**
     * @Description: 添加部分为：获取用户信息，以此判断是登陆前开店还是登录后开店，决定营业执照填写页面中的返回按钮的返回页面
     * @param shopId 商铺id
     * @param req 请求
     * @return 跳转到营业执照填写页面
     * @author huangls 1449700087@qq.com
     * @date 2016年8月16日
     */
    @RequestMapping(value = "gotoOpenShop", method = RequestMethod.GET)
    public String gotoOpenShop(Model model, String yourName, String yourPhone, Integer province, Integer city, Integer district,
            String describe, String shop, String cellId, HttpServletRequest req) {
    	
    	//获取当前登陆的用户的信息
        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        
		model.addAttribute("user", user);
        model.addAttribute("provinceList",districtInfoService.getDistrictInfoByLevel((int)Constants.MENU_LEVEL_FIREST));
        model.addAttribute("contactor", yourName);
        model.addAttribute("contactorPhone", yourPhone);
        model.addAttribute("provinceId", province);
        model.addAttribute("cityId", city);
        model.addAttribute("areaId", district);
        model.addAttribute("cellId", cellId);
        model.addAttribute("descrip", describe);
        model.addAttribute("shopCategoryId", shop);
        return "openShop";
    }
    
    /**
     * @description 保存营业执照信息
     * @author chenxin
     * @date 2016年8月15日上午9:31:22
     */
    @RequestMapping(value = "saveLisence", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveLisence(String contactor, String contactorPhone, Integer provinceId, 
    		Integer cityId, Integer areaId, String cellId, String descrip, String shopCategoryId, String shopName, 
    		String companyName, String businessLicense, String legalName, String legalIdcard, String legalIdcardPhoto, 
    		String businessLicenseProvinceid,String  businessLicenseCityid, String businessLicenseAreaid, 
    		String businessLicenseAddress, String registerDate,String beginDate, String endDate, Integer registerMoney,
    		String businessScope,String businessLicensePhoto,Integer companyProvinceid,Integer companyCityid,
            Integer companyAreaid, String companyAddress,String companyPhone,String urgentContactor,String urgentContactorPhone) {
        
        RequestJson result =new RequestJson();
        try{
            
        ShopInfo shopInfo = new ShopInfo();
        shopInfo.setId(UniqueNumberUtils.get18UniqueNumber());
        shopInfo.setContactor(contactor);
        shopInfo.setContactorPhone(contactorPhone);
        shopInfo.setProvinceId(provinceId);
        shopInfo.setCityId(cityId);
        shopInfo.setAreaId(areaId);
        shopInfo.setCellId(cellId);
        shopInfo.setDescrip(descrip);
        shopInfo.setShopCategoryId(shopCategoryId);
        shopInfo.setName(shopName);
        shopInfo.setCompanyName(companyName);
        shopInfo.setBusinessLicense(businessLicense);
        shopInfo.setLegalName(legalName);
        shopInfo.setLegalIdcard(legalIdcard);
        shopInfo.setLegalIdcardPhoto(legalIdcardPhoto);
        shopInfo.setBusinessLicenseProvinceid(businessLicenseProvinceid);
        shopInfo.setBusinessLicenseCityid(businessLicenseCityid);
        shopInfo.setBusinessLicenseAreaid(businessLicenseAreaid);
        shopInfo.setBusinessLicenseAddress(businessLicenseAddress);
        shopInfo.setRegisterDate(DateUtils.stringToDate(registerDate));
        shopInfo.setBeginDate(DateUtils.stringToDate(beginDate));
        shopInfo.setEndDate(DateUtils.stringToDate(endDate));
        shopInfo.setRegisterMoney(registerMoney);
        shopInfo.setBusinessScope(businessScope);
        shopInfo.setBusinessLicensePhoto(businessLicensePhoto);
        shopInfo.setCompanyProvinceid(companyProvinceid);
        shopInfo.setCompanyCityid(companyCityid);
        shopInfo.setCompanyAreaid(companyAreaid);
        shopInfo.setCompanyAddress(companyAddress);
        shopInfo.setCompanyPhone(companyPhone);
        shopInfo.setUrgentContactor(urgentContactor);
        shopInfo.setUrgentContactorPhone(urgentContactorPhone);
        shopInfo.setState(NEW_SHOP);
        
        shopInfoService.insert(shopInfo);
        result.setMsg("保存成功！");
        result.setSuccess(true);
        
        } catch(Exception e) {
            result.setMsg("保存异常");
            result.setSuccess(false);
        }
        return result;
    }
    
    /**
     * @description 检验此小区是否具有缴纳停车费的权限
     * @param cellId 小区id
     * @author chenxin
     * @date 2016年8月31日下午6:54:39
     */
    @RequestMapping(value = "checkCarRightByCellId", method = RequestMethod.GET)
    @ResponseBody
    public RequestJson checkCarRightByCellId(HttpServletRequest request) {
        RequestJson result = new RequestJson();
        User user = (User)request.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        CellInfo cellInfo = user.getCurrentCell();
        String cellId = cellInfo.getId();
        
        List<String> list = cellInfoService.getMenusByCellId(cellId, Constants.SYSTEM_OWNER);
        
        for (String item : list) {
            Menu menu = menuService.getMenuById(item);
            String value = menu.getValue();
            if(StringUtils.equals(value, "owner.car")) {
                result.setSuccess(true);
                return result;
            }  
        }
        
        result.setSuccess(false);
        result.setMsg("此小区没有缴纳停车费的权限！！！");
        return result;
        
    }
    
    /**
     * @description 检验此小区是否具有缴纳物业费的权限
     * @param cellId 小区id
     * @author chenxin
     * @date 2016年8月31日下午6:54:39
     */
    @RequestMapping(value = "checkPropertyRightByCellId", method = RequestMethod.GET)
    @ResponseBody
    public RequestJson checkPropertyRightByCellId(HttpServletRequest request) {
        RequestJson result = new RequestJson();
        User user = (User)request.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        CellInfo cellInfo = user.getCurrentCell();
        String cellId = cellInfo.getId(); 
        
        List<String> list = cellInfoService.getMenusByCellId(cellId, Constants.SYSTEM_OWNER);
        
        for (String item : list) {
            Menu menu = menuService.getMenuById(item);
            String value = menu.getValue();
            if (StringUtils.equals(value, "owner.propertyFee")) {
                result.setSuccess(true);
                return result;
            }
        }
        
        result.setSuccess(false);
        result.setMsg("此小区没有缴纳物业费的权限！！！");
        return result;
    }
}
